package com.neuedu.sprinttx;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.neuedu.sprinttx")
@EnableTransactionManagement   //扫描 Transactional
public class Appconfig {


    @Bean
    DataSource getDataSource(){

        DruidDataSource datasource = new DruidDataSource();
        datasource.setUsername("root");
        datasource.setPassword("root");
        datasource.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis");
        datasource.setDriverClassName("com.mysql.jdbc.Driver");

        //其他配置
        //datasource.set
        return datasource;
    }

    @Bean
    JdbcTemplate getJdbcTemplate(DataSource dataSource ){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }


    @Bean("txManager")
    DataSourceTransactionManager txManager(DataSource dataSource ){
        DataSourceTransactionManager txManager = new DataSourceTransactionManager(dataSource);
        return txManager;
    }



}
